package com.atguigu.gulimall.order.config;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 在SpringBoot中可以使用@Bean注解来创建MQ,容器中的Binding,Exchange,Queue在队列中如果不存在的话会直接创建出来
 * 如果RabbitMQ中有上述,即使使用@Bean创建的属性发生了变化,RabbitMQ中也不会变
 *
 * @title: MyMQConfig
 * @Author syh
 * @Date: 2022/7/14 21:09
 * @Version 1.0
 */

@Configuration
public class MyMQConfig {

    @Bean
    public Queue orderDelayQueue() {
        Map<String, Object> arguments = new HashMap<>();
        /**
         * x-dead-letter-exchange: order-event-exchange
         * x-dead-letter-routing-key: order.release.order
         * x-message-ttl: 60000
         */
        arguments.put("x-dead-letter-exchange", "order-event-exchange");
        arguments.put("x-dead-letter-routing-key", "order.release.order");
        arguments.put("x-message-ttl", 60000);

        // String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments
        Queue queue = new Queue("order.delay.queue", true, false, false, arguments);

        return queue;
    }

    @Bean
    public Queue orderReleaseOrderQueue() {
        // String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments
        Queue queue = new Queue("order.release.order.queue", true, false, false);

        return queue;
    }

    @Bean
    public Exchange orderEventExchange() {
        // String name, boolean durable, boolean autoDelete, Map<String, Object> arguments
        return new TopicExchange("order-event-exchange", true, false);
    }

    @Bean
    public Binding orderCreateOrderBinding() {
        /**
         * String destination: 目的地,相当于是队列
         * DestinationType destinationType: 目的地类型
         * String exchange: 交换机的名称
         * String routingKey: 路由键
         * Map<String, Object> arguments: 其他参数
         */
        return new Binding("order.delay.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.order",
                null);
    }

    @Bean
    public Binding orderReleaseOrderBinding() {
        /**
         * String destination: 目的地,相当于是队列
         * DestinationType destinationType: 目的地类型
         * String exchange: 交换机的名称
         * String routingKey: 路由键
         * Map<String, Object> arguments: 其他参数
         */
        return new Binding("order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",
                null);
    }

    /**
     * 订单释放直接和库存失败进行绑定
     *
     * @return
     */
    @Bean
    public Binding orderReleaseOtherBinding() {
        /**
         * String destination: 目的地,相当于是队列
         * DestinationType destinationType: 目的地类型
         * String exchange: 交换机的名称
         * String routingKey: 路由键
         * Map<String, Object> arguments: 其他参数
         */
        return new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.other.#",
                null);
    }

    @Bean
    public Queue orderSeckillOrderQueue(){
        /**
         * String name: 队列名称
         * boolean durable: 是否持久化
         * boolean exclusive: 是否排他(同一时间只能有一个用户使用)
         * boolean autoDelete: 是否自动删除
         */
        Queue queue = new Queue("order.seckill.order.queue", true, false, false);
        return queue;
    }

    @Bean
    public Binding orderSeckillOrderQueueBinding() {
        /**
         * String destination: 目的地
         * DestinationType destinationType: 目的地类型
         * String exchange: 交换机名称
         * String routingKey: 路由键
         * Map<String, Object> arguments: 其他参数
         */
        Binding binding = new Binding("order.seckill.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.seckill.order",
                null);
        return binding;
    }

}
